package li.brauch.semesterarbeit.radio.views.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class ProgressBar extends Canvas {
		
	private long value = Long.MIN_VALUE;
	private long minimum = Long.MAX_VALUE;
	private long maximum = Long.MIN_VALUE;
	
	public ProgressBar(Composite parent, int style) {
		super(parent, style);
		
		addPaintListener(new PaintListener() {
	         public void paintControl(PaintEvent e) {
	        	 ProgressBar.this.paintControl(e);
	         }
	     });
	}
	
	void paintControl(PaintEvent e) {

	     GC gc = e.gc;
	     
	     Color oldColor = gc.getForeground();
	     
	     int width = e.width;
	     int height = e.height;
	     
	     Rectangle rectangle = new Rectangle(0, 0, width - 1, height - 1);
	     gc.setForeground(getForeground());
	     gc.drawRectangle(rectangle);
	     gc.setForeground(oldColor);
	     
	     int progressWidth = (int) (((width  - 4) * value) / maximum);
	     int progressHeight =  height - 4;
	     
	     rectangle = new Rectangle(2, 2, progressWidth, progressHeight);
	     	     
	     oldColor = gc.getBackground();
	     gc.setBackground(getForeground());
	     gc.fillRectangle(rectangle);
	     gc.setBackground(oldColor);
	}

	public Point computeSize(int wHint, int hHint, boolean changed) {
	    
		Point size = getSize();
		
		int width = Math.max(40 + 2, size.x);
		int height = Math.max(6 + 2, size.y);
	     
	    if (wHint != SWT.DEFAULT) width = wHint;
	    if (hHint != SWT.DEFAULT) height = hHint;
	
	    return new Point(width, height);     
	}
	
	public long getValue() {
		return value;
	}

	public void setValue(long value) {
		this.value = value;
		redraw();
	}

	public long getMinimum() {
		return minimum;
	}

	public void setMinimum(long minimum) {
		this.minimum = minimum;
	}

	public long getMaximum() {
		return maximum;
	}

	public void setMaximum(long maximum) {
		this.maximum = maximum;
	}

	
	
	

}
